﻿//
// GNS_Operator.cpp
// MobileManage
//
// Created by mr.l on 16/2/1.
//
//

#include "GNS_Operator.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_Common.h"
#include "GNS_PlayerMgr.h"
#include "GnsOften.h"
#include "SystemMgr.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

GNS_Operator::GNS_Operator()
{
    pGNS_Operator=0;
    pListView=nullptr;
    pPromoter=nullptr;
}

GNS_Operator::~GNS_Operator()
{
    
}

GNS_Operator* GNS_Operator::createLayer()
{
    auto pGNS_Operator = GNS_Operator::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_Operator);
    return pGNS_Operator;
}

bool GNS_Operator::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_Operator::SetNodeName(std::string CsbName,int agentname)
{
    pGNS_Operator = CSLoader::createNode(CsbName);
    pGNS_Operator->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_Operator);
    addChild(pGNS_Operator,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_Operator);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_Operator", pGNS_Operator);

    auto pBackBtn = static_cast<Button*>(pGNS_Operator->getChildByName("OperatorBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_Operator::BtnCallBack, this));
    
    pListView=static_cast<ListView*>(pGNS_Operator->getChildByName("ListView_1"));
    pPromoter = static_cast<Text*>(pGNS_Operator->getChildByName("Text_8"));
	pPromoter->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram(StringUtils::format("代理商账号:%d",agentname)));
}
void GNS_Operator::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_Operator");

                auto pAccounted=GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
                pAccounted->setVisible(true);

				//GNS_NodeMgr::getInstance()->setChildVisible("GNS_Accounted",true);
                break;
            }
            default:
                break;
        }
    }
}

void GNS_Operator::ShowDate()
{
    pListView->removeAllItems();
    int x=0;
    Size bgSize;
    for (auto it:*(GNS_PlayerMgr::getInstance()->getExListRes1())) {
        pPromoter->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram(StringUtils::format("代理商账号:%d",it._uplineCode)));
        auto BgImage = ImageView::create("line2.png");
        pListView->addChild(BgImage);
        bgSize = BgImage->getContentSize();
        
        auto pName  = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        pName->setPosition(Vec2(bgSize.width*0.11, bgSize.height*0.28));
        pName->setColor(Color3B::BLACK);
        BgImage->addChild(pName);
        
        auto pAcc = Text::create(StringUtils::format("%d",it._agentPercent), GameFontName, 24);
        pAcc->setPosition(Vec2(bgSize.width*0.35, bgSize.height*0.28));
        pAcc->setColor(Color3B::BLACK);
        BgImage->addChild(pAcc);
        
        auto pAcc1 =Text::create(StringUtils::format("%d",it._lowlinePercent), GameFontName, 24);
        pAcc1->setPosition(Vec2(bgSize.width*0.58, bgSize.height*0.28));
        pAcc1->setColor(Color3B::BLACK);
        BgImage->addChild(pAcc1);
        
        auto pPlayer = Text::create(StringUtils::format("%d",it._memberNum), GameFontName, 24);
        pPlayer->setPosition(Vec2(bgSize.width*0.87, bgSize.height*0.28));
        pPlayer->setColor(Color3B::BLACK);
        BgImage->addChild(pPlayer);
        x++;
    }
    pListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*x));
}